home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / grep / grep.lzh / getopt.c < prev    next >
C/C++ Source or Header  |  1989-03-02  |  17KB  |  491 lines

  1. /* Getopt for GNU.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4.                NO WARRANTY
  5.  
  6.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16.  
  17.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  18. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  19. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  20. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  21. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  22. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  23. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  24. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  25. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  27.  
  28.         GENERAL PUBLIC LICENSE TO COPY
  29.  
  30.   1. You may copy and distribute verbatim copies of this source file
  31. as you receive it, in any medium, provided that you conspicuously and
  32. appropriately publish on each copy a valid copyright notice "Copyright
  33.  (C) 1987 Free Software Foundation, Inc."; and include following the
  34. copyright notice a verbatim copy of the above disclaimer of warranty
  35. and of this License.  You may charge a distribution fee for the
  36. physical act of transferring a copy.
  37.  
  38.   2. You may modify your copy or copies of this source file or
  39. any portion of it, and copy and distribute such modifications under
  40. the terms of Paragraph 1 above, provided that you also do the following:
  41.  
  42.     a) cause the modified files to carry prominent notices stating
  43.     that you changed the files and the date of any change; and
  44.  
  45.     b) cause the whole of any work that you distribute or publish,
  46.     that in whole or in part contains or is a derivative of this
  47.     program or any part thereof, to be licensed at no charge to all
  48.     third parties on terms identical to those contained in this
  49.     License Agreement (except that you may choose to grant more
  50.     extensive warranty protection to third parties, at your option).
  51.  
  52.     c) You may charge a distribution fee for the physical act of
  53.     transferring a copy, and you may at your option offer warranty
  54.     protection in exchange for a fee.
  55.  
  56.   3. You may copy and distribute this program or any portion of it in
  57. compiled, executable or object code form under the terms of Paragraphs
  58. 1 and 2 above provided that you do the following:
  59.  
  60.     a) cause each such copy to be accompanied by the
  61.     corresponding machine-readable source code, which must
  62.     be distributed under the terms of Paragraphs 1 and 2 above; or,
  63.  
  64.     b) cause each such copy to be accompanied by a
  65.     written offer, with no time limit, to give any third party
  66.     free (except for a nominal shipping charge) a machine readable
  67.     copy of the corresponding source code, to be distributed
  68.     under the terms of Paragraphs 1 and 2 above; or,
  69.  
  70.     c) in the case of a recipient of this program in compiled, executable
  71.     or object code form (without the corresponding source code) you
  72.     shall cause copies you distribute to be accompanied by a copy
  73.     of the written offer of source code which you received along
  74.     with the copy you received.
  75.  
  76.   4. You may not copy, sublicense, distribute or transfer this program
  77. except as expressly provided under this License Agreement.  Any attempt
  78. otherwise to copy, sublicense, distribute or transfer this program is void and
  79. your rights to use the program under this License agreement shall be
  80. automatically terminated.  However, parties who have received computer
  81. software programs from you with this License Agreement will not have
  82. their licenses terminated so long as such parties remain in full compliance.
  83.  
  84.   5. If you wish to incorporate parts of this program into other free
  85. programs whose distribution conditions are different, write to the Free
  86. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  87. worked out a simple rule that can be stated here, but we will often permit
  88. this.  We will be guided by the two goals of preserving the free status of
  89. all derivatives of our free software and of promoting the sharing and reuse of
  90. software.
  91.  
  92.  
  93. In other words, you are welcome to use, share and improve this program.
  94. You are forbidden to forbid anyone else to use, share and improve
  95. what you give them.   Help stamp out software-hoarding!  */
  96.  
  97. /* This version of `getopt' appears to the caller like standard Unix `getopt'
  98.    but it behaves differently for the user, since it allows the user
  99.    to intersperse the options with the other arguments.
  100.  
  101.    As `getopt' works, it permutes the elements of `argv' so that,
  102.    when it is done, all the options precede everything else.  Thus
  103.    all application programs are extended to handle flexible argument order.
  104.  
  105.    Setting the environment variable _POSIX_OPTION_ORDER disables permutation.
  106.    Then the behavior is completely standard.
  107.  
  108.    GNU application programs can use a third alternative mode in which
  109.    they can distinguish the relative order of options and other arguments.  */
  110.  
  111. #include <stdio.h>
  112.  
  113. #ifdef sparc
  114. #include <alloca.h>
  115. #endif
  116. #ifdef USG
  117. #define bcopy(s, d, l) memcpy((d), (s), (l))
  118. #endif
  119.  
  120. /* For communication from `getopt' to the caller.
  121.    When `getopt' finds an option that takes an argument,
  122.    the argument value is returned here.
  123.    Also, when `ordering' is RETURN_IN_ORDER,
  124.    each non-option ARGV-element is returned here.  */
  125.  
  126. char *optarg = 0;
  127.  
  128. /* Index in ARGV of the next element to be scanned.
  129.    This is used for communication to and from the caller
  130.    and for communication between successive calls to `getopt'.
  131.  
  132.    On entry to `getopt', zero means this is the first call; initialize.
  133.  
  134.    When `getopt' returns EOF, this is the index of the first of the
  135.    non-option elements that the caller should itself scan.
  136.  
  137.    Otherwise, `optind' communicates from one call to the next
  138.    how much of ARGV has been scanned so far.  */
  139.  
  140. int optind = 0;
  141.  
  142. /* The next char to be scanned in the option-element
  143.    in which the last option character we returned was found.
  144.    This allows us to pick up the scan where we left off.
  145.  
  146.    If this is zero, or a null string, it means resume the scan
  147.    by advancing to the next ARGV-element.  */
  148.  
  149. static char *nextchar;
  150.  
  151. /* Callers store zero here to inhibit the error message
  152.    for unrecognized options.  */
  153.  
  154. int opterr = 1;
  155.  
  156. /* Describe how to deal with options that follow non-option ARGV-elements.
  157.  
  158.    UNSPECIFIED means the caller did not specify anything;
  159.    the default is then REQUIRE_ORDER if the environment variable
  160.    _OPTIONS_FIRST is defined, PERMUTE otherwise.
  161.  
  162.    REQUIRE_ORDER means don't recognize them as options.
  163.    Stop option processing when the first non-option is seen.
  164.    This is what Unix does.
  165.  
  166.    PERMUTE is the default.  We permute the contents of `argv' as we scan,
  167.    so that eventually all the options are at the end.  This allows options
  168.    to be given in any order, even with programs that were not written to
  169.    expect this.
  170.  
  171.    RETURN_IN_ORDER is an option available to programs that were written
  172.    to expect options and other ARGV-elements in any order and that care about
  173.    the ordering of the two.  We describe each non-option ARGV-element
  174.    as if it were the argument of an option with character code zero.
  175.    Using `-' as the first character of the list of option characters
  176.    requests this mode of operation.
  177.  
  178.    The special argument `--' forces an end of option-scanning regardless
  179.    of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
  180.    `--' can cause `getopt' to return EOF with `optind' != ARGC.  */
  181.  
  182. static enum { REQUIRE_ORDER, PERMUTE,